package com.itheima.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.poi.POIUtils;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "预约管理")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    @PostMapping("ordersetting/importOrderSettings")
    @ApiOperation(value = "预约管理上传excel数据",notes = "预约管理上传excel数据将数据写入数据库")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file){
        try {

            List<String[]> list = POIUtils.readExcel(file);
            orderSettingService.importOrderSettings(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @GetMapping("ordersetting/findSettingDate/{year}/{month}")
    @ApiOperation(value = "查询预约信息的方法", notes = "依据年月查询当月所有预约信息")
    public Result findSettingDate(@PathVariable("year")int year,@PathVariable("month")int month){
        try{
            Map map = orderSettingService.findSettingDate(year,month);
            return new Result(true,MessageConstant.QUERY_ORDERSETTING_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
    }

    @GetMapping("ordersetting/updateSettingDate/{s}/{number}")
    @ApiOperation(value = "修改可预约总数的方法",notes = "根据日期修改当日的可预约总数")
    public Result updateSettingDate(@PathVariable("s")String s,@PathVariable("number")int number){
        try{
            orderSettingService.updateSettingDate(s,number);
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
    }
}
